//
//  monster.c
//  
//
//  Created by Thomas Maillard on 28/03/13.
//
//

#include <time.h>
#include "../include/game.h"
#include "../include/map.h"
#include "../include/player.h"
#include "../include/misc.h"
#include "../include/level.h"
#include "../include/monster.h"



struct s_monster {
	int x, y;
	SDL_Surface * directed_img[4];
	enum e_way current_way;

};

t_monster monster_init() {
	t_monster player = malloc(sizeof(*player));
	if (!player)
		error("Memory error");


	player->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
	player->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
	player->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
	player->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);





	player->current_way = SOUTH;

	return player;
}

void monster_display(t_monster player, SDL_Surface *screen) {
	player->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);

//fprintf(stderr,"je rentre");

		SDL_Rect positionimage;
		positionimage.x = 30;
		positionimage.y= 30;



		/*
		 *
		 *SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
				&positionimage);

*/
		int where = rand()%4;
		//fprintf(stderr,"Voici mon chiffre thomas %d\n",where);
		switch (where) {
		case 1:
			player->current_way = EAST;
			positionimage.x += 10;

			SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
							&positionimage);
			break;

		case 2:
			player->current_way = SOUTH;
			positionimage.y += 10;
			SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
							&positionimage);
			break;
		case 3:
			player->current_way = NORTH;
			positionimage.y -= 10;
			SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
							&positionimage);
			break;
		case 0:
			player->current_way = WEST;
			positionimage.x -= 10;
			SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
							&positionimage);
			break;

		}
}

void monster_from_map(t_monster player, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_MONSTER) {
				player->x = i;
				player->y = j;
			}
		}
	}
}



static int monster_move_aux(t_monster player, t_map map, int x1, int y1, int x2,
		int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0; //0 to avoid collision with scenery (trees, rocks,..)
		break;

	case CELL_CASE:
		return 0; // 0 to avoid collision with boxes
		break;

	case CELL_BONUS:
		break;

	case CELL_GOAL:
		break;

	case CELL_MONSTER:
		break;

	case CELL_PLAYER:
		break;

	default:
		break;
	}

	// Player has moved
	return 1;
}







int monster_move(t_monster player, t_map map) {
	int x = player->x;
	int y = player->y;
	int move = 0;

	switch (player->current_way) {
	case NORTH:
		if (monster_move_aux(player, map, x, y - 1, x, y - 2)) {
			player->y--;

			//if (y<=0) {player->y++;}//Joueur bloqué par le Nord

			move = 1;
		}
		break;

	case SOUTH:
		if (monster_move_aux(player, map, x, y + 1, x, y + 2)) {
			player->y++;

			//if (y>=map_get_height(map)-1) {player->y--;}//Joueur bloqué par le Sud

			move = 1;
		}
		break;

	case WEST:
		if (monster_move_aux(player, map, x - 1, y, x - 2, y)) {
			player->x--;

			//if (x<=0) {player->x++;}//Joueur bloqué par l'Ouest

			move = 1;
		}
		break;

	case EAST:
		if (monster_move_aux(player, map, x + 1, y, x + 2, y)) {
			player->x++;

			//if (x>=(map_get_width(map)-1)) {player->x--;}//Joueur bloqué par l'Est

			move = 1;
		}
		break;
	}

	//La mise en commentaires suivante permet de pouvoir prendre les escaliers (passer sur la case des escaliers sans les faire disparaître)
	/*
	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, player->x, player->y, CELL_PLAYER);
	}
	*/
	return move;
}

/*
struct s_monster {
		int x, y;
		SDL_Surface * directed_img[4];
		enum e_way current_way;
		int move_left;
		};

t_monster monster_init(int bomb_number,int life_number) {
	t_monster monster = malloc(sizeof(*monster));
	if (!monster)
		error("Memory error");
	monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
	monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
	monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
	monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);
	monster->move_left=0;
	monster->current_way = SOUTH;

	return monster;
}

int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->x;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->y;
}

void monster_set_current_way(t_monster monster, enum e_way way) {
	assert(monster);
	monster->current_way = way;
}


static int monster_move_aux(t_monster monster, t_map map, int x1, int y1, int x2, int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0;
		break;

	case CELL_KEY:
		return 0;
		break;

	case CELL_CASE:
		return 0;
		break;

	case CELL_BOMB_TTL4:
		return 0;
		break;

	case CELL_BONUS:
		return 0;
		break;

	case CELL_DOOR:
		return 0;
		break;

	case CELL_CLOSED_DOOR:
		return 0;
		break;

	case CELL_GOAL:
		return 0;
		break;

	case CELL_MONSTER:
		break;

	case CELL_PLAYER:
		return 1;
		break;

	default:
		break;
	}

	// monster has moved
	return 1;
}



int monster_move(t_monster monster, t_map map) {
	int x = monster->x;
	int y = monster->y;
	int move = 0;

	switch (monster->current_way) {
	case NORTH:
		if (monster_move_aux(monster, map, x, y - 1, x, y - 2)) {
			monster->y--;
			move = 1;
			monster->move_left=4;
		}
		break;

	case SOUTH:
		if (monster_move_aux(monster, map, x, y + 1, x, y + 2)) {
			monster->y++;
			move = 1;
			monster->move_left=4;
		}
		break;

	case WEST:
		if (monster_move_aux(monster, map, x - 1, y, x - 2, y)) {
			monster->x--;
			move = 1;
			monster->move_left=4;
		}
		break;

	case EAST:
		if (monster_move_aux(monster, map, x + 1, y, x + 2, y)) {
			monster->x++;
			move = 1;
			monster->move_left=4;
		}
		break;
	}

	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, monster->x, monster->y, CELL_MONSTER);
	}
	return move;
}
*/

/*
int life_number = 3;
struct s_monster {
		int x, y;
		SDL_Surface * directed_img[4];
		enum e_way current_way;
		uint original_time; // Add Timer see display monster
		uint true_time;
		int fast_monster;
		int nb_bomb;
		int nb_life;
		int move_left;
};

t_monster monster_init(int bomb_number,int life_number) {
	t_monster monster = malloc(sizeof(*monster));
	if (!monster)
		error("Memory error");
	monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
	monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
	monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
	monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);

	monster->current_way = SOUTH;
	monster->nb_bomb = bomb_number;
	monster->nb_life = life_number;
	monster->original_time =SDL_GetTicks();
	monster->true_time=SDL_GetTicks();
	monster->fast_monster = 1000;
	monster->move_left=0;
	return monster;
}

int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->x;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->y;
}

void monster_set_current_way(t_monster monster, enum e_way way) {
	assert(monster);
	monster->current_way = way;
}


// ADDED
int monster_get_nb_life(t_monster monster) {
	assert(monster);
	return monster->nb_life;
}

void monster_inc_nb_life(t_monster monster) {
	assert(monster);
	monster->nb_life += 1;
}

void monster_dec_nb_life(t_monster monster) {
	assert(monster);
	monster->nb_life -= 1;
}

int monster_from_map(t_monster monster, t_map map) {
	int i=0, j=0;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_MONSTER) {
				monster->x = i;
				monster->y = j;
				map_set_cell_type(map,i,j,CELL_EMPTY);
				return 1;
				}
		}
	}
	return 0;
}



static int monster_move_aux(t_monster monster, t_map map, int x1, int y1, int x2, int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0;
		break;

	case CELL_KEY:
		return 0;
		break;

	case CELL_CASE:
		return 0;
		break;

	case CELL_BOMB_TTL4:
		return 0;
		break;

	case CELL_BONUS:
		return 0;
		break;

	case CELL_DOOR:
		return 0;
		break;

	case CELL_CLOSED_DOOR:
		return 0;
		break;

	case CELL_GOAL:
		return 0;
		break;

	case CELL_MONSTER:
		break;

	case CELL_PLAYER:
		return 1;
		break;

	default:
		break;
	}

	// monster has moved
	return 1;
}


//Making move of the monster automatic using function RAND


int monster_move(t_monster monster, t_map map) {
	int x = monster->x;
	int y = monster->y;
	int move = 0;

	switch (monster->current_way) {
	case NORTH:
		if (monster_move_aux(monster, map, x, y - 1, x, y - 2)) {
			monster->y--;
			move = 1;
			monster->move_left=4;
		}
		break;

	case SOUTH:
		if (monster_move_aux(monster, map, x, y + 1, x, y + 2)) {
			monster->y++;
			move = 1;
			monster->move_left=4;
		}
		break;

	case WEST:
		if (monster_move_aux(monster, map, x - 1, y, x - 2, y)) {
			monster->x--;
			move = 1;
			monster->move_left=4;
		}
		break;

	case EAST:
		if (monster_move_aux(monster, map, x + 1, y, x + 2, y)) {
			monster->x++;
			move = 1;
			monster->move_left=4;
		}
		break;
	}

	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, monster->x, monster->y, CELL_MONSTER);
	}
	return move;
}


void monster_display(t_monster monster, SDL_Surface *screen) {

	assert(monster);
	assert(monster->directed_img[monster->current_way]);
	assert(screen);

	if(monster->nb_life == 3){
			monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
			monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
			monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
			monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);
		}
		else if(monster->nb_life == 2){
			monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
			monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
			monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
			monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);
		}
		else{
			monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
			monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
			monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
			monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);
		}

	SDL_Rect place;
	if(monster->current_way == NORTH){
		switch(monster->move_left){
		case 4:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y+0.8) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 3:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y+0.6) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 2:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y+0.4) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 1:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y+0.2) * SIZE_BLOC;
			monster->move_left--;
			break;
		default:
			place.x = monster->x * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
		break;
		}
	}
	else if(monster->current_way == SOUTH){
		switch(monster->move_left){
		case 4:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y-0.8) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 3:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y-0.6) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 2:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y-0.4) * SIZE_BLOC;
			monster->move_left--;
			break;
		case 1:
			place.x = (monster->x) * SIZE_BLOC;
			place.y = (monster->y-0.2) * SIZE_BLOC;
			monster->move_left--;
			break;
		default:
			place.x = monster->x * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
		break;
		}
	}
	else if(monster->current_way == EAST){
		switch(monster->move_left){
		case 4:
			place.x = (monster->x-0.8) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 3:
			place.x = (monster->x-0.6) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 2:
			place.x = (monster->x-0.4) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 1:
			place.x = (monster->x-0.2) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		default:
			place.x = monster->x * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
		break;
		}
	}
	else if(monster->current_way == WEST){
		switch(monster->move_left){
		case 4:
			place.x = (monster->x+0.8) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 3:
			place.x = (monster->x+0.6) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 2:
			place.x = (monster->x+0.4) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		case 1:
			place.x = (monster->x+0.2) * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
			monster->move_left--;
			break;
		default:
			place.x = monster->x * SIZE_BLOC;
			place.y = monster->y * SIZE_BLOC;
		break;
		}
	}

	SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,
			&place);

}

*/





























/*
void monster_display(SDL_Surface *screen,t_player player) {

	assert(screen);

	SDL_Rect place;
	int monster[4];
	monster[1]= load_image(IMG_MONSTER_LEFT );
	monster[2] = load_image(IMG_MONSTER_LEFT );
	monster[3] = load_image(IMG_MONSTER_LEFT );
	monster[4] = load_image(IMG_MONSTER_LEFT );


	{

					int where = rand()%4;
					fprintf(stderr,"Voici mon chiffre thomas %d\n",where);
					switch (where) {
					case 1:
						map_set_cell_type(map,3,3,CELL_MONSTER_UP);
						break;

					case 2:
						map_set_cell_type(map,3,3,CELL_MONSTER_DOWN);
						break;
					case 3:
						map_set_cell_type(map,3,3,CELL_MONSTER_LEFT);
						break;
					case 0:
						map_set_cell_type(map,3,3,CELL_MONSTER_RIGHT);
						break;

					}

		SDL_BlitSurface(monster->length_img[3], NULL, screen, &place);


	}
	*/
/*
struct list_monster {
	t_player monster;	//The monster has the same structure as a player
	t_listmonster next;
};


static t_listmonster game_listmonster;

// Set the static listmonster
void monster_set_listmonster(t_listmonster listmonster) {
	game_listmonster = listmonster;
}

//Get the static listmonster
t_listmonster monster_get_listmonster() {
	return game_listmonster;
}

//return the next listmonster from a listmonster
t_listmonster monster_next (t_listmonster listmonster) {
	return listmonster->next;
}


//Manage the list of monsters
t_listmonster monster_set(t_listmonster listmonster, int x, int y) {

	if (listmonster == NULL) //First time a monster is set
		listmonster = monster_create(x, y); //There is an only monster
	else {
		listmonster->next=monster_set(listmonster->next, x, y);
		}					//We check the list until the NULL next
	return listmonster;
}



t_listmonster monster_create(int x, int y) {

	t_player monster;

	//monster = player_init(0,3);  //The monster is create the same way that a player is
								// 3 indicates enables to set the right images and a nb_life = 0


	monster = player_init(0);


	player_set_x(monster, x);
	player_set_y(monster, y);

	t_listmonster newlistmonster= malloc(sizeof(struct list_monster)); //We create a new list

	newlistmonster->monster=monster;  //The monster is set in the newlistmonster
	newlistmonster->next = NULL;      //We carefully initialize next

	return newlistmonster;
}


// Kill all the monster on a cell
t_listmonster monster_kill(int x, int y, t_listmonster listmonster) {

		if (listmonster != NULL) { //We stop at the first NULL

			//if ((player_get_x(listmonster->monster) == x) && (player_get_y(listmonster->monster) == y)) {
				player_monster_nb_life(listmonster->monster, -1);
			}		//A monster is killed by setting his its nb_life to -1


			listmonster->next = monster_kill(x,y,listmonster->next);
					//We call the function on the next listmonter to check all the monsters
		}

		return listmonster;
}

// Return the monster of a listmonster
t_player monster_get_monster(t_listmonster listmonster) {
	return listmonster->monster;
}


//Return the nb of monsters on a cell
int monster_on_cell(int x, int y) {

	t_listmonster listmonster = game_listmonster;

	int nb_monster = 0;

	while (nb_monster < 2 && listmonster != NULL) { //If there is still at least one other monster not dead on the cell
		//if ((player_get_x(listmonster->monster) == x) && (player_get_y(listmonster->monster) == y) && player_get_nb_life(listmonster->monster)>0)
		//	nb_monster += 1;
		if ((player_get_x(listmonster->monster) == x) && (player_get_y(listmonster->monster) == y)>0)
			nb_monster += 1;
		listmonster = listmonster->next;
	}
	return nb_monster;
}


//Display all the monsters on the screen
void monster_display(t_listmonster listmonster, SDL_Surface *screen) {


	assert(screen);

	//SDL_Rect place;

	while (listmonster != NULL) {

	//if (player_get_nb_life(listmonster->monster) == 0) { //Only the living monsters are displayed
	place.x = player_get_x(listmonster->monster) * SIZE_BLOC;
	place.y = player_get_y(listmonster->monster) * SIZE_BLOC + SIZE_BLOC + SIZE_LINE;

	//SDL_BlitSurface(player_get_directed_img(listmonster->monster, player_get_current_way(listmonster->monster)), NULL, screen,
		//	&place);

	//SDL_BlitSurface(player_get_directed_img(listmonster->monster), NULL, screen,&place);
	//}boucle de fin du IF
	listmonster=listmonster->next;

	}
}


void monster_save(FILE * fichier) {


	int nb_monster=0;
	t_listmonster listmonster = monster_get_listmonster(); //Get the static listmonster

	while (listmonster != NULL) {
		nb_monster += 1;			// Count the number of monsters displayed
		listmonster=listmonster->next;
	}

	fprintf(fichier, "%d\n", nb_monster); //Save it

	listmonster = monster_get_listmonster();

	int i;
	int mx;
	int my;

	for (i=0; i<nb_monster; i++) {

		t_player monster;
		monster = listmonster->monster;  //We get the monster inside the list
		mx=player_get_x(monster);
		my=player_get_y(monster);

		fprintf(fichier, "%d:%d\n", mx,my); //Save each monster's data

		listmonster = listmonster->next;


	}
}


void monster_load_monster(FILE *fichier) {

	int i=0;
	int x=0;
	int y=0;
	int nb_monster;
	if (fscanf(fichier, "%d\n", &nb_monster) == -1)
			error("Can't open the monster data save : %s\n", SDL_GetError()); //We know the number of monsters saved

	for(i=0; i<nb_monster; i++) {
		if (fscanf(fichier, "%d:%d\n", &x, &y) == -1)
			error("Can't open the monster data save : %s\n", SDL_GetError());

		monster_set_listmonster(monster_set(monster_get_listmonster(), x, y));
					//The monster is add to the static listmonster on the CELL(x,y)
	}
}

*/
/*
struct s_monster {
	int x, y;
	SDL_Surface * directed_img[4];
	enum e_way current_way;

	//int nb_bomb;
};




static int monster_move_aux(t_monster monster, t_map map, int x1, int y1, int x2,
		int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0; //0 to avoid collision with scenery (trees, rocks,..)
		break;

	case CELL_CASE:
		return 0; // 0 to avoid collision with boxes
		break;

	case CELL_BONUS:
		break;

	case CELL_GOAL:
		break;

	case CELL_MONSTER:
		break;

	case CELL_PLAYER:
		break;

	default:
		break;
	}

	// Player has moved
	return 1;
}


int monster_move(t_monster monster, t_map map) {
	int x = monster->x;
	int y = monster->y;
	int move = 0;

	switch (monster->current_way) {
	case NORTH:
		if (monster_move_aux(monster, map, x, y - 1, x, y - 2)) {
			monster->y--;

			//if (y<=0) {monster->y++;}//Joueur bloqué par le Nord

			move = 1;
		}
		break;

	case SOUTH:
		if (monster_move_aux(monster, map, x, y + 1, x, y + 2)) {
			monster->y++;

			//if (y>=map_get_height(map)-1) {monster->y--;}//Joueur bloqué par le Sud

			move = 1;
		}
		break;

	case WEST:
		if (monster_move_aux(monster, map, x - 1, y, x - 2, y)) {
			monster->x--;

			//if (x<=0) {monster->x++;}//Joueur bloqué par l'Ouest

			move = 1;
		}
		break;

	case EAST:
		if (monster_move_aux(monster, map, x + 1, y, x + 2, y)) {
			monster->x++;

			//if (x>=(map_get_width(map)-1)) {monster->x--;}//Joueur bloqué par l'Est

			move = 1;
		}
		break;
	}

	//La mise en commentaires suivante permet de pouvoir prendre les escaliers (passer sur la case des escaliers sans les faire disparaître)

	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, monster->x, monster->y, CELL_monster);
	}

	return move;
}

void monster_display(t_monster monster, SDL_Surface *screen) {
	//assert(monster);
	//assert(monster->directed_img[monster->current_way]);
	//assert(screen);

	SDL_Rect place;
	place.x = monster->x * SIZE_BLOC;
	place.y = monster->y * SIZE_BLOC;

	SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,&place);
}



*/


/*
struct list_monster_ {
	t_monster monster;	//The monster has the same structure as a monster
	t_listmonster next;
};

static t_listmonster game_listmonster;

t_listmonster monster_create(int x, int y) {

	t_monster monster;

	monster = monster_init(0,2);  //The monster is create the same way that a monster is
								// 2 indicates enables to set the right images and a nb_life = 0
	monster_set_x(monster, x);
	monster_set_y(monster, y);

	t_listmonster newlistmonster= malloc(sizeof(struct list_monster_)); //We create a new list

	newlistmonster->monster=monster;  //The monster is set in the newlistmonster
	newlistmonster->next = NULL;      //We carefully initialize next

	return newlistmonster;
}

//Display all the monsters on the screen
void monster_display(t_listmonster listmonster, SDL_Surface *screen) {


	assert(screen);

	SDL_Rect place;

	while (listmonster != NULL) {

	if (monster_get_nb_life(listmonster->monster) == 0) { //Only the living monsters are displayed
	place.x = monster_get_x(listmonster->monster) * SIZE_BLOC;
	place.y = monster_get_y(listmonster->monster) * SIZE_BLOC + SIZE_BLOC + SIZE_LINE;

	//SDL_BlitSurface(monster_get_directed_img(listmonster->monster, monster_get_current_way(listmonster->monster)), NULL, screen, &place);

	listmonster=listmonster->next;

	}
}
*/




//On crée un élément de la liste chainée
/*
struct s_monster {
	int monster_x, monster_y;
	SDL_Surface *directed_img_monster;
    
};

struct ListeMonster {
	t_monster monster;
	list_monster next;
};

*/

/*
 t_bomb set_bomb(int x,int y,float timer, int power)
 {
 
 t_bomb bomb = malloc(sizeof(*bomb));
 if(!bomb)
 error("Memory error");
 
 bomb->directed_img_bomb[0] = load_image(IMG_BOMB_TTL1);//Load image
 bomb->directed_img_bomb[1] = load_image(IMG_BOMB_TTL2);
 bomb->directed_img_bomb[2] = load_image(IMG_BOMB_TTL3);
 bomb->directed_img_bomb[4] = load_image(IMG_BOMB_TTL4);
 
 bomb->timer = timer;
 bomb->power = power;
 bomb->bomb_x=x;
 bomb->bomb_y=y;
 return bomb;
 }
 */
/*
 void monster_display(t_monster monster, SDL_Surface *screen) {
 assert(monster);
 assert(screen);
 
 SDL_Rect monster_place;
 monster_place.x = monster->monster_x * SIZE_BLOC;
 monster_place.y = monster->monster_y * SIZE_BLOC;
 
 SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,
 &place);
 }
 */
/*
void monster_display(t_monster monster, SDL_Surface *screen) {
	assert(monster);
	assert(screen);
	SDL_Rect monster_place;
    
	monster_place.x = monster->monster_x * SIZE_BLOC;
	monster_place.y = monster->monster_y * SIZE_BLOC;
 */
	//Here we set the time for the bomb to explode and the sprites associated
	//float timer;
	//float one_second = (1000/30);
	//timer=bomb->timer;
    
	//if (timer >= 3*one_second )
	/*{
		SDL_BlitSurface(monster->directed_img_monster, NULL, screen, &monster_place);
		
	}*/
	/*
	 if ((timer >= 2 * one_second) && (timer < 3 * one_second) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[2], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }
     
	 if ( (timer >= 1 * one_second) && (timer < 2 * one_second) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[1], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }
     
	 if ((timer < 1 * one_second) && (timer >=0) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[0], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }
     
     
	 if ((timer < 0) && (timer >= -1 * (one_second) ))
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 int explosion_range;
	 for (explosion_range=0 ; bomb->power >= explosion_range; explosion_range++ )
	 {
	 bomb_place.x = (bomb ->bomb_x) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y + explosion_range) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y - explosion_range) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x + explosion_range) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y ) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x - explosion_range) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y ) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 }
	 }

}
*/
/*
list_monster monster_create(t_monster monster, t_map map) {
    
    map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER);
    
    
	list_monster list = malloc(sizeof(*list));   //We create a new list of monsters
	t_monster monster = malloc(sizeof(*monster));			//And a new monster
	//FILE *fichier = NULL;
	int x = monster_get_x(monster);
	int y = monster_get_y(monster);
*/
    //
	//int i=1;
	/*
	 switch (monster_get_current_way(monster)) {
     
	 case NORTH:
	 bomb->bomb_x=x;
     
	 bomb->bomb_y=y - i +1;
	 break;
     
	 case SOUTH:
	 bomb->bomb_x=x;
     
	 bomb->bomb_y=y + i -1;
	 break;
     
	 case WEST:
     
	 bomb->bomb_x=x - i + 1;
	 bomb->bomb_y=y;
	 break;
     
	 case EAST:
	 bomb->bomb_x=x + i -1;
	 bomb->bomb_y=y;
	 break;
	 //	}
	 *//*

	monster->monster_x = x;
	monster->monster_y = y;
	//map_set_cell_type(map, bomb->bomb_x, bomb->bomb_y, CELL_BOMB_TTL4); //The cell becomes a CELL_BOMB
    

    
	list->monster = monster;
	list->next = NULL;
 
	list_print_monster(list);
    return list;*//*
}
     FILE *fichier = NULL;
     fichier = fopen("bar.txt", "w");
     fprintf(fichier, "%d", bomb->bomb_x);
     fclose(fichier);
     */

	//fprintf(fichier,"2");
	//fclose(fichier);
	//return newlist;
//}
/*
void print_list_element_monster(t_monster monster) {
    
	printf("monster : x=%d:y=%d\n", monster->monster_x, monster->monster_y);
}

void list_print_monster(list_monster l) {
	if (l) {
        
		print_list_element_monster(l->monster);
		list_print_monster(l->next);
	}
}
*/
/*
void move_monster(liste_monster monster)
{
    while(1<2) do{
        monster->monster_x=x-1;
        
    
    }
    return EXIT_SUCCESS;
}
*/
 //Initialisation de la liste de monstres
 /*
 list_monster monsterInitialisation()
 {
 
 list_monster liste=malloc(sizeof(*list_monster));
 list element=malloc(sizeof(*element));
 
 if (liste == NULL || element == NULL)
 {
 exit(EXIT_FAILURE);
 }
 
 element->monster_x = 0;
 element->monster_y = 0;
 
 element->suivant = NULL;
 // liste->premier = element;
 
 return list_monster;
 }
 
 
 

 
 //Ajouter un élément en tête de liste
 
 void monsterInsertion(list_monster liste, t_monster monster)
 {
 //Création du nouvel élément
 list nouveau = malloc(sizeof(*nouveau));
 if (liste == NULL || nouveau == NULL)
 {
 exit(EXIT_FAILURE);
 }
 
 nouveau->monster_x= monster_get_x(monster);
 nouveau->monster_y= monster_get_y(monster);
 
 //Insertion de l'élément au début de la liste
 nouveau->suivant = liste->premier;
 liste -> premier = nouveau;
 
 }
 
 
 
 //Affichage de la liste chaînée
 
 void bombeAfficherListe_monster(t_monster monster, list_monster liste)
 {
 if (liste == NULL)
 {
 exit(EXIT_FAILURE);
 }
 
 list actuel = liste->premier;
 
 while ( actuel != NULL )
 {
 printf("x=%d;y=%d -> ",actuel->monster_x,actuel->monster_y);
 
 actuel=actuel->suivant;
 }
 printf("NULL\n");
 }
 
 
 void bombPrint(t_monster monster, t_map map, list_bomb liste)
 {
 
 //liste = bombeInitialisation();
 //bombeInsertion(liste,monster);
 
 //bombeAfficherListe(monster,liste);
 
 map_set_cell_type(map, monster_get_x(monster),monster_get_y(monster), CELL_BOMB);
 
 
 }
 
 
 */

/*
 // Test if the list is empty
 int list_is_empty (list l) {
 return !l;
 }
 
 
 // Creates a new list with one element
 list list_new (t_monster monster)
 {
 list nouvelle_bomb = malloc (sizeof(*nouvelle_bomb));
 assert (nouvelle_bomb);
 nouvelle_bomb->bomb_x = monster_get_x(monster);
 nouvelle_bomb->bomb_y = monster_get_y(monster);
 
 nouvelle_bomb->suivant = NULL;
 return nouvelle_bomb;
 }
 
 
 // Add a new element at begining of l, returns the new list
 list list_add (list liste, t_monster monster) {
 list l1 = list_new (monster);
 if (list_is_empty(liste))
 return l1;
 l1->suivant = liste;
 return l1;
 }
 
 
 // Print one list element
 void print_list_element (int bomb_x,t_monster monster)
 {
 
 printf ("x=%d:y=%d ", monster_get_x(monster),monster_get_y(monster));
 }
 
 
 // Print each element of the list (recursive
 void list_print (list l,t_monster monster) {
 if (!list_is_empty(l)) {
 print_list_element(l->bomb_x,monster);
 list_print(l->suivant,monster);
 }
 }
 
 */

/*
 list bomb_create (t_monster monster, t_map map) {
 
 list newlist= malloc(sizeof(struct list_bomb_));   //We create a new list
 bomb bomb= malloc(sizeof(struct bomb_));			//And a new bomb
 
 int x=monster_get_x(monster);
 int y=monster_get_y(monster);
 
 
 
 //int bomb_throw = monster_get_bomb_throw(monster); //We get the current bomb_throw
 int i=1;
 
 switch (monster_get_current_way(monster)) {
 
 case NORTH:
 bomb->bomb_x=x;    //While check everything, especially if the bomb meets a tree with bomb_ban_explode
 while (map_is_inside(map, x, y - i) )
 {
 i += 1;
 }
 bomb->bomb_y=y - i +1; //We set the bomb position to the last cell OK
 break;
 
 case SOUTH:
 bomb->bomb_x=x;
 while (map_is_inside(map, x, y + i)) {
 i += 1;
 }
 bomb->bomb_y=y + i -1;
 break;
 
 case WEST:
 while (map_is_inside(map, x - i, y))
 {i += 1;}
 bomb->bomb_x=x - i + 1;
 bomb->bomb_y=y;
 break;
 
 case EAST:
 while (map_is_inside(map, x + i, y) )
 {i += 1;}
 bomb->bomb_x=x + i -1;
 bomb->bomb_y=y;
 break;
 }
 
 map_set_cell_type(map, bomb->bomb_x, bomb->bomb_y, CELL_BOMB); //The cell becomes a CELL_BOMB
 
 
 newlist->bomb=bomb;			//The bomb is set in the newlist
 newlist->next = NULL;		//We carefully initialize next
 
 return newlist;
 } 
 */

/*
 
 struct s_bomber {
 int x, y;
 SDL_Surface* img_bomb; //Load image bomb
 //enum e_way current_way;
 
 int nb_bomb;
 };
 
 void bomb_display(t_bomb bomb, SDL_Surface *screen) {
 assert(bomb);
 assert(bomb->img_bomb);
 assert(screen);
 
 SDL_Rect place;
 place.x = bomb->x * SIZE_BLOC;
 place.y = bomb->y * SIZE_BLOC;
 
 SDL_BlitSurface(bomb->img_bomb, NULL, screen,&place);
 }
 */


